package com.itheima.reggie.controller;

/**
 * @author 程达
 * @PackageName:com.itheima.reggie.controller
 * @ClassName:ShoppingCartController
 * @Description:
 * @date 2022/5/29 16:30
 **/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
@Api(tags = "购物车相关接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * A. 获取当前登录用户，为购物车对象赋值
     * B. 根据当前登录用户ID及本次添加的菜品ID/套餐ID查询购物车数据
     * C. 如果已经存在，就在原来数量基础上加1
     * D. 如果不存在，则添加到购物车，数量默认就是1
     *
     * @param shoppingCart 购物车
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增购物车接口")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //1.设置用户id，指定当前是哪个用户的购物车数据,页面没传入这个字段
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //2.拼接查询sqlSQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        //2.1根据当前登录用户ID及本次添加的菜品ID/套餐ID查询购物车数据  查询当前菜品或者套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());//判断是不是当前用户,拼接sql
        //2.2判断添加到购物车的是菜品 就两种结果，要么是菜品要是套餐(套餐和菜品都是有很多种类的，所以要继续请判断)
        if (shoppingCart.getDishId() != null) {
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //2.3判断添加到购物车的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //3.判断有没有这个数据  判断当前购物车是否存在
        ShoppingCart cartServiceOne = shoppingCartService.getOne(wrapper);
        if (cartServiceOne != null) {
            //如果已经存在，就在原来数量基础上加一,改变统计字段上面的值
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
            //再执行一下修改的方法，存到库中
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //3.2购物车没有这个购物项 添加页面传过来的参数中没有的字段
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            //4.把有set好属性对象，重新给这个购物项返回，用来保存
            cartServiceOne = shoppingCart;
        }
        // 返回这个购物项
        return R.success(cartServiceOne);
    }

    //3.减少购物车项处理器  sub请求地址
    @PostMapping("/sub")
    @ApiOperation(value = "减少购物车项接口")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //1.设置用户id，指定当前是哪个用户的购物车数据,页面没传入这个字段
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //2.拼接查询sqlSQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        //2.1根据当前登录用户ID及本次添加的菜品ID/套餐ID查询购物车数据  查询当前菜品或者套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());//判断是不是当前用户,拼接sql
        //2.2判断要删除的购物车的是菜品 就两种结果，要么是菜品要是套餐(套餐和菜品都是有很多种类的，所以要继续请判断)
        if (shoppingCart.getDishId() != null) {
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //2.3判断添加到购物车的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //3.判断有没有这个数据  判断当前购物车是否存在
        ShoppingCart cartServiceOne = shoppingCartService.getOne(wrapper);
        if (cartServiceOne != null && cartServiceOne.getNumber()>1) {
            //如果已经存在，就在原来数量基础上加一,改变统计字段上面的值
            cartServiceOne.setNumber(cartServiceOne.getNumber() - 1);
            //再执行一下修改的方法，存到库中
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //3.2购物车的购物项 添加页面传过来的参数中没有的字段
           shoppingCartService.removeById(cartServiceOne);
            //4.把有set好属性对象，重新给这个购物项返回，用来保存
            cartServiceOne = shoppingCart;
        }
        // 返回这个购物项
        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查看购物车所有购物项接口")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }


    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation(value = "清空购物车接口")
    public R<String> clean() {
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
}